package com.taotao.portal.service.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpUtils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.po.TbUser;
import com.taotao.pojo.TaotaoResult;
import com.taotao.portal.service.UserService;
import com.taotao.util.CookieUtils;
import com.taotao.util.HttpClientUtil;
import com.taotao.util.JsonUtils;
/**
 * 	用户接口
 *  @author  DaiRui  
 *  @ClassName  : UserServiceImpl
 *  @Description:
 *  @date  2017年4月17日 上午9:56:44 
 *  @Version  1.0
 */
@Service
public class UserServiceImpl implements UserService {

	@Value("${SSO_BASE_URL}")
	private String SSO_BASE_URL;
	
	@Value("${SSO_USER_TOKEN_SERVICE}")
	private String SSO_USER_TOKEN_SERVICE;
	
	@Override
	public TbUser getUserByToken(HttpServletRequest request,
			HttpServletResponse response) {
		
		// 从浏览器获取cookie
		String cookie = CookieUtils.getCookieValue(request, "TT_TOKEN");
		if (StringUtils.isBlank(cookie)) {
			return null;
		}
		// 如果cookie不为空，调用单点系统的服务
		String json = HttpClientUtil.doGet(SSO_BASE_URL+SSO_USER_TOKEN_SERVICE+cookie);
		
		TaotaoResult result = TaotaoResult.formatToPojo(json, TbUser.class);
		if (result.getStatus()!=200) {
			return null;
		}
		// 获取用户的数据
		TbUser user = (TbUser) result.getData();		
		return user;
	}

}
